﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Data.Common;
using System.Xml;
using System.IO;
using Apq.DB;

namespace Apq.Windows.Forms
{
	/// <summary>
	/// 数据库连接对话框
	/// </summary>
	public partial class DBConnector : DockForm
	{
		// 点击TabPage时改变其值,用于返回数据库连接
		private DBProduct _DBSelected = DBProduct.MySql;
		/// <summary>
		/// 获取数据库连接类型
		/// </summary>
		public DBProduct DBSelected
		{
			get { return _DBSelected; }
		}
		/// <summary>
		/// 获取数据库连接
		/// </summary>
		public DbConnection DBConnection
		{
			get
			{
				switch (_DBSelected)
				{
					case DBProduct.MySql:
						return _MySqlConnection;
					case DBProduct.MsSql:
					default:
						return _SqlConnection;
				}
			}
		}

		/// <summary>
		/// 数据库连接对话框
		/// </summary>
		public DBConnector()
		{
			InitializeComponent();
		}

		/// <summary>
		/// 设置界面语言值
		/// </summary>
		public override void SetUILang(Apq.UILang.UILang UILang)
		{
			this.Text = UILang["连接到数据库"];

			#region MySql
			tabPage1.Text = UILang["MySql"];
			mySqlConnector1.SetUILang(UILang);
			btnMySqlConfirm.Text = UILang["确定(&O)"];
			btnMySqlCancel.Text = UILang["取消(&C)"];
			#endregion

			#region MsSql
			tabPage2.Text = UILang["MsSql"];
			btnMsSqlConfirm.Text = UILang["确定(&O)"];
			btnMsSqlCancel.Text = UILang["取消(&C)"];
			#endregion

			base.SetUILang(UILang);
		}

		private void DBConnector_Load(object sender, EventArgs e)
		{
		}

		private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (tabControl1.SelectedIndex == 0)
			{
				_DBSelected = DBProduct.MySql;
			}
			if (tabControl1.SelectedIndex == 1)
			{
				_DBSelected = DBProduct.MsSql;
			}
		}

		#region MySql
		// 确定
		public void btnMySqlConfirm_Click(object sender, EventArgs e)
		{
			try
			{
				_MySqlConnection = mySqlConnector1.CreateMySqlConnection();
				_MySqlConnection.Open();

				DialogResult = System.Windows.Forms.DialogResult.OK;
				Close();
			}
			catch (System.Exception ex)
			{
				MessageBox.Show(this, ex.Message);
			}
			finally
			{
				DbConnectionHelper.Close(_MySqlConnection);
			}
		}
		// 取消
		private void btnMySqlCancel_Click(object sender, EventArgs e)
		{
			DialogResult = System.Windows.Forms.DialogResult.Cancel;
			Close();
		}
		#endregion

		#region MsSql
		// 确定
		public void btnMsSqlConfirm_Click(object sender, EventArgs e)
		{
			try
			{
				_SqlConnection = msSqlConnector1.CreateMsSqlConnection();
				_SqlConnection.Open();

				DialogResult = System.Windows.Forms.DialogResult.OK;
				Close();
			}
			catch (System.Exception ex)
			{
				MessageBox.Show(this, ex.Message);
			}
			finally
			{
				DbConnectionHelper.Close(_SqlConnection);
			}
		}
		// 取消
		private void btnMsSqlCancel_Click(object sender, EventArgs e)
		{
			DialogResult = System.Windows.Forms.DialogResult.Cancel;
			Close();
		}
		#endregion
	}
}
